package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.ui.util.AdjustablePanel;

import javax.swing.*;

public class GDCharStatEditPane extends AdjustablePanel {
    private GDCharDataPane pnlCharData;
    private GDCharAttributePane pnlAttribute;
    private GDCharPointPane pnlPoints;
    private GDCharEditTabbedPane tabPane;

    public GDCharStatEditPane(final GDCharEditTabbedPane tabPane) {
        this.tabPane = tabPane;
        this.adjustUI();
        this.setChar(null);
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel pnlMain = this.buildMainPanel();
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlMain));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlMain));
        layout.setVerticalGroup(vGroup);
    }

    @Override
    public void adjustUI() {
        if (this.pnlCharData == null) {
            this.pnlCharData = new GDCharDataPane(this.tabPane, this);
        } else {
            this.pnlCharData.adjustUI();
        }
        if (this.pnlAttribute == null) {
            this.pnlAttribute = new GDCharAttributePane(this);
        } else {
            this.pnlAttribute.adjustUI();
        }
        if (this.pnlPoints == null) {
            this.pnlPoints = new GDCharPointPane(this.tabPane);
        } else {
            this.pnlPoints.adjustUI();
        }
    }

    private JPanel buildMainPanel() {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlCharData)).addGroup(layout.createParallelGroup().addComponent(this.pnlAttribute)).addGroup(layout.createParallelGroup().addComponent(this.pnlPoints));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlCharData).addComponent(this.pnlAttribute).addComponent(this.pnlPoints));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlCharData, this.pnlAttribute);
        layout.linkSize(0, this.pnlCharData, this.pnlPoints);
        layout.linkSize(1, this.pnlCharData, this.pnlAttribute);
        layout.linkSize(1, this.pnlCharData, this.pnlPoints);
        return panel;
    }

    public void setChar(final GDChar gdc) {
        this.pnlCharData.setChar(gdc);
        this.pnlAttribute.setChar(gdc);
        this.pnlPoints.setChar(gdc);
    }

    public void updateChar(final GDChar gdc) {
        if (gdc == null) {
            return;
        }
        this.pnlCharData.updateChar(gdc);
        this.pnlAttribute.updateChar(gdc);
        this.pnlPoints.updateChar(gdc);
    }

    public String getCharName() {
        if (this.pnlCharData == null) {
            return null;
        }
        return this.pnlCharData.getCharName();
    }

    public int getStatPoints() {
        if (this.pnlPoints == null) {
            return 0;
        }
        return this.pnlPoints.getStatPoints();
    }

    public float getHealth() {
        if (this.pnlAttribute == null) {
            return 0.0f;
        }
        return this.pnlAttribute.getHealth();
    }

    public float getEnergy() {
        if (this.pnlAttribute == null) {
            return 0.0f;
        }
        return this.pnlAttribute.getEnergy();
    }

    public void setStatPoints(final int points) {
        if (this.pnlPoints == null) {
            return;
        }
        this.pnlPoints.setStatPoints(points);
    }

    public void setSkillPoints(final int points) {
        if (this.pnlPoints == null) {
            return;
        }
        this.pnlPoints.setSkillPoints(points);
    }

    public void setHealth(final float health) {
        if (this.pnlAttribute == null) {
            return;
        }
        this.pnlAttribute.setHealth(health);
    }

    public void setEnergy(final float energy) {
        if (this.pnlAttribute == null) {
            return;
        }
        this.pnlAttribute.setEnergy(energy);
    }

    public void resetMastery(final String masteryID) {
        if (this.pnlPoints != null) {
            this.pnlPoints.resetMastery(masteryID);
        }
    }
}
